package com.xingzhi.shortvideosharingplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingzhi.shortvideosharingplatform.dto.FavoritesFolderDTO;
import com.xingzhi.shortvideosharingplatform.mapper.FavoritesFolderMapper;
import com.xingzhi.shortvideosharingplatform.model.FavoritesFolder;
import com.xingzhi.shortvideosharingplatform.service.FavoritesFolderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FavoritesFolderServiceImpl extends ServiceImpl<FavoritesFolderMapper, FavoritesFolder> implements FavoritesFolderService {

    @Override
    @Transactional
    public Long createFolder(FavoritesFolderDTO folderDTO) {
        FavoritesFolder folder = new FavoritesFolder();
        BeanUtils.copyProperties(folderDTO, folder);
        
        // 设置时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 两个时间
        folder.setCreatedTime(currentTime);
        folder.setUpdatedTime(currentTime);
        
        // 插入数据库
        save(folder);
        
        return folder.getId();
    }
    
    @Override
    @Transactional
    public Long createFolder(Long userId, String folderName) {
        FavoritesFolder folder = new FavoritesFolder();
        folder.setUserId(userId);
        folder.setFolderName(folderName);
        folder.setIsPrivate(0L); // 默认公开
        folder.setSortOrder(0L);  // 默认排序
        
        // 设置时间
        LocalDateTime currentTime = LocalDateTime.now();
        folder.setCreatedTime(currentTime);
        folder.setUpdatedTime(currentTime);
        
        // 插入数据库
        save(folder);
        
        return folder.getId();
    }

    @Override
    @Transactional
    public boolean updateFolder(FavoritesFolderDTO folderDTO) {
        // 检查收藏夹是否存在且属于当前用户
        FavoritesFolder existingFolder = getById(folderDTO.getId());
        if (existingFolder == null || !existingFolder.getUserId().equals(folderDTO.getUserId())) {
            return false;
        }
        
        // 更新收藏夹信息
        BeanUtils.copyProperties(folderDTO, existingFolder);
        //  更新时间
        existingFolder.setUpdatedTime(LocalDateTime.now());
        
        return updateById(existingFolder);
    }

    @Override
    @Transactional
    public boolean deleteFolder(Long folderId, Long userId) {
        // 检查收藏夹是否存在且属于当前用户
        LambdaQueryWrapper<FavoritesFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FavoritesFolder::getId, folderId)
                    .eq(FavoritesFolder::getUserId, userId);
        
        return remove(queryWrapper);
    }

    @Override
    public List<FavoritesFolder> getUserFolders(Long userId) {
        LambdaQueryWrapper<FavoritesFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FavoritesFolder::getUserId, userId)
                    .orderByAsc(FavoritesFolder::getSortOrder)
                    .orderByDesc(FavoritesFolder::getUpdatedTime);
        
        return list(queryWrapper);
    }

    @Override
    public FavoritesFolder getFolderDetail(Long folderId, Long userId) {
        LambdaQueryWrapper<FavoritesFolder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FavoritesFolder::getId, folderId)
                    .eq(FavoritesFolder::getUserId, userId);
        
        return getOne(queryWrapper);
    }
} 